#!/usr/bin/env python
# encoding: utf-8
"""
@author:zwb
@file: messages.py
@contact: zwb15083976291@163.com
@time: 2022/03/10
"""
from rest_framework import serializers

from common.models import BlogMessage
from utils.serializers import SpecifySerializeFieldModelSerializer, SpecifySerializeFieldSerializer
from utils.serializers.serializer import UpdateBaseSerializeMethodMixin, PaginatorSerializer


class MessagesSerializers(UpdateBaseSerializeMethodMixin, SpecifySerializeFieldModelSerializer):
    id = serializers.IntegerField()
    audit = serializers.ChoiceField(choices=[0, 1, 2])

    class Meta:
        model = BlogMessage
        fields = '__all__'

    def validate_id(self, value):
        if not BlogMessage.objects.filter(id=value).exists():
            raise serializers.ValidationError("id不存在")

        return value


class MessagesPaginatorSerializer(UpdateBaseSerializeMethodMixin, PaginatorSerializer):
    audit = serializers.ChoiceField(choices=[0, 1, 2])


class MessagesBatchSerializers(UpdateBaseSerializeMethodMixin, SpecifySerializeFieldSerializer):
    ids = serializers.ListField()
    audit = serializers.ChoiceField(choices=[1, 2])

    def validate_ids(self, value: list):
        if BlogMessage.objects.filter(id__in=value).count() != len(value):
            raise serializers.ValidationError("id不存在")
        return value
